package com.tfswx.jcyw.print.listview;

import com.tfswx.jcyw.print.bean.FileItem;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * @author 张述江
 * @date 2023-11-01
 */
public class CustomListCell extends ListCell<FileItem> {


    private final Consumer<FileItem> deleteClick;
    private final BiConsumer<FileItem, MouseEvent> itemClick;

    public CustomListCell(Consumer<FileItem> deleteClick, BiConsumer<FileItem, MouseEvent> itemClick) {
        super();
        this.deleteClick = deleteClick;
        this.itemClick = itemClick;
    }

    @Override
    protected void updateItem(FileItem item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setGraphic(null);
        } else {
            CheckBox checkBox = new CheckBox();
            Label name = new Label();
            name.setWrapText(false);
            name.setMaxWidth(280);
            Label pageCount = new Label();

            BorderPane content = new BorderPane();
            content.setPadding(new Insets(10, 0, 10, 0));
            setGraphic(content);
            content.setLeft(checkBox);
            content.setCenter(new Pane(name));

            AnchorPane anchorPane = new AnchorPane();
            anchorPane.getChildren().add(pageCount);
            Button button = new Button();
            button.setVisible(false);
            button.setLayoutX(0);
            button.setLayoutY(-3);
            button.setPrefWidth(24);
            button.setPrefHeight(24);
            button.setCursor(Cursor.HAND);
            button.setStyle("-fx-background-image: url(\"image/remove.png\"); -fx-background-size: 100%,100%; -fx-background-repeat: no-repeat; -fx-background-position: center,center; -fx-background-color: transparent;");
            button.onMouseMovedProperty().set(event -> button.setStyle("-fx-background-image: url(\"image/remove-hover.png\"); -fx-background-size: 100%,100%; -fx-background-repeat: no-repeat; -fx-background-position: center,center; -fx-background-color: transparent;"));
            button.onMouseExitedProperty().set(event -> button.setStyle("-fx-background-image: url(\"image/remove.png\"); -fx-background-size: 100%,100%; -fx-background-repeat: no-repeat; -fx-background-position: center,center; -fx-background-color: transparent;"));
            button.setOnMouseClicked(event -> deleteClick.accept(item));
            anchorPane.getChildren().add(button);
            content.setRight(anchorPane);

            content.onMouseMovedProperty().set(event -> {
                button.setVisible(true);
                pageCount.setVisible(!button.isVisible());
            });
            content.onMouseExitedProperty().set(event -> {
                button.setVisible(false);
                pageCount.setVisible(!button.isVisible());
            });

            content.setOnMouseClicked(event -> itemClick.accept(item, event));

            name.setText(item.getName());
            name.setTooltip(new Tooltip(name.getText()));
            pageCount.setText(item.getPageCount().getValue() + "页");
            checkBox.selectedProperty().bindBidirectional(item.getSelectProperty());

        }
    }
}

